package com.itheima.线程安全.Lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        Thread t1 = new Thread(ticket);
        Thread t2 = new Thread(ticket);
        Thread t3 = new Thread(ticket);

        t1.setName("窗口一");
        t2.setName("窗口二");
        t3.setName("窗口三");

        t1.start();
        t2.start();
        t3.start();
    }
}


class Ticket implements Runnable{

private int ticket=50;
private Lock lock=new ReentrantLock();

    @Override
    public void run() {
        while (true){
            try {
                //加锁
                lock.lock();
                if (ticket<=0){
                    //卖完了
                    break;
                }else {
                    //睡眠
                    Thread.sleep(100);
                    ticket--;
                    System.out.println(Thread.currentThread().getName()+"在卖票，还剩下"+ticket+"张票");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
            }
        }


    }
}